﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Windows.Media.Animation;
using DOMercury.Interfaces;


namespace Bezel__
{

    /// <summary>
    /// Interaction logic for BezelListSelector.xaml
    /// </summary>
    public partial class ActionList : Window
    {
        bool currentlyVisible = false;
        private ListSelectorType type;

        public ActionList(ListSelectorType ListType)
        {
            InitializeComponent();
            type = ListType;
            itemsListBox.ItemsSource = new List<IAction>();
            MouseLeftButtonDown += new MouseButtonEventHandler(OnMouseMove);
            itemsListBox.SelectionChanged += new SelectionChangedEventHandler(itemsListBox_SelectionChanged);


        }

        void itemsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!CurrentlyVisible || e.AddedItems.Count == 0)
                return;

            switch (type)
            {
                case ListSelectorType.Item:
                    DOMercuryCoreManager.Instance.CurrentItem = ((IItem)(itemsListBox.SelectedItem)); break;
                case ListSelectorType.Action:
                    DOMercuryCoreManager.Instance.CurrentAction = ((IAction)(itemsListBox.SelectedItem)); break;
                default:
                    DOMercuryCoreManager.Instance.CurrentParam = ((IItem)(itemsListBox.SelectedItem)); break;

            }
            
            if (itemsListBox.SelectedItem != null)
            {
                itemsListBox.ScrollIntoView(itemsListBox.SelectedItem);
            }

        }

        void OnMouseMove(object sender, MouseButtonEventArgs e)
        {
            DragMove();
        }

        public void IncrementItem()
        {
            if (itemsListBox.ItemsSource != null)
            {
                if (itemsListBox.SelectedIndex < ((List<IAction>)(itemsListBox.ItemsSource)).Count)
                {
                    itemsListBox.SelectedIndex++;
                }
            }
        }

        public void DecrementItem()
        {
            if (itemsListBox.ItemsSource != null)
            {
                if (itemsListBox.SelectedIndex > 0)
                {
                    itemsListBox.SelectedIndex--;
                }
            }
        }

        public List<IAction> ItemsSource
        {
            get
            {
                return itemsListBox.ItemsSource as List<IAction>;
            }
            set
            {
                itemsListBox.ItemsSource = value;
                if (value.Count > 0)
                {
                    itemsListBox.SelectedIndex = 0;
                }
            }
        }

        public IAction SelectedItem
        {
            get
            {
                return itemsListBox.SelectedItem as IAction;
            }
            set
            {
                itemsListBox.SelectedItem = value;
            }

        }

        public bool CurrentlyVisible
        {
            get
            {
                return currentlyVisible;
            }
            set
            {
                currentlyVisible = value;
                Opacity = (currentlyVisible) ? 1 : 0;

                if (currentlyVisible && !IsVisible) // first show
                    Visibility = Visibility.Visible;

                if (currentlyVisible)
                    Windows.FocusHelper.sendWindowToFront(this);
            }
        }

        public void SetDragMove(Window parentWindow)
        {
            parentWindow.MouseDown += new MouseButtonEventHandler(parentWindow_MouseDown);
        }

        void parentWindow_MouseDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        internal void ClearItemsSource()
        {
            //((List<IAction>)(itemsListBox.ItemsSource)) = null;
        }
    }
}
